import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class ServerMain {
    private static Logger log = Logger.getLogger(ServerMain.class.getName());
    private static String[] sharesNames;

    public static void main(String[] args) {
        start();
    }

    public static void loadLoggerProperties() {
        try {
            LogManager.getLogManager().readConfiguration(
                    ServerMain.class.getResourceAsStream("/logger.properties"));
        } catch (IOException e) {
            System.err.println("Could not setup logger configuration: " + e.toString());
        }
    }

    /**
     * Loads configuration parameters.
     */
    public static void loadConfigFile() {

        Properties config = new Properties();
        String filename = "resources/config.properties";

        try {
            config.load(new FileInputStream(filename));
        } catch (FileNotFoundException e) {
            log.severe("Config file not found");
            return;
        } catch (IOException ex) {
            log.severe("Config file not opened");
            return;
        }

        sharesNames = config.get("shares").toString().split(",");
    }

    /**
     * Start server
     */
    private static void start() {
        loadLoggerProperties();
        loadConfigFile();
        Server server = new Server(sharesNames);
        Thread serverThread = new Thread(server);
        serverThread.start();
    }
}
